<template>
  <div ref="target">
    <home-panel title="热门品牌" sub-title="国际经典 品质保证">
      <template v-slot:right>
        <a
          href="javascript:;"
          class="iconfont icon-angle-left"
          :class="{ disabled: flag }"
          @click="leftFn"
        ></a>
        <a
          href="javascript:;"
          class="iconfont icon-angle-right"
          :class="{ disabled: !flag }"
          @click="rightFn"
        ></a>
      </template>
      <div class="box" ref="box">
        <!-- 动画组件- 显示 -->
        <transition name="fade">
          <ul
            class="list"
            :class="{ right: !flag, left: flag }"
            v-if="list.length > 1"
          >
            <li v-for="item in list" :key="item.id">
              <router-link to="/">
                <img :src="item.picture" alt="" />
              </router-link>
            </li>
          </ul>
          <!-- 骨架 -->
          <div class="home-skeleton" v-else>
            <div
              class="item"
              v-for="i in 5"
              :key="i"
              style="backgroundcolor: #f0f9f4"
            >
              <XtxSkeleton bg="#e4e4e4" width="240px" height="306px" animated />
            </div>
          </div>
        </transition>
      </div>
    </home-panel>
  </div>
</template>

<script>
import HomePanel from './homePanel.vue'
import { getHotBrandListAPI } from '@/api'
import { ref, computed } from 'vue'
import { useLazyData } from '@/utils/hooks'
export default {
  name: 'HomeBrands',
  components: { HomePanel },
  setup () {
    const flag = ref(true) // 按钮禁用
    // 懒加载
    const { target, res } = useLazyData(getHotBrandListAPI, { limit: 10 })
    const list = computed(() => {
      if (res.value) {
        return res.value.result
      } else {
        return []
      }
    })
    // 左按钮
    const leftFn = () => {
      flag.value = true
    }
    // 右按钮
    const rightFn = () => {
      flag.value = false
    }
    return {
      list,
      flag,
      leftFn,
      rightFn,
      target
    }
  }
}
</script>

  <style scoped lang="less">
.home-panel {
  background: #f5f5f5;
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: var(--xtx-color);
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;
    li {
      margin-right: 10px;
      width: 240px;
      &:nth-child(5n) {
        margin-right: 0;
      }
      img {
        width: 240px;
        height: 305px;
      }
    }
  }
}

.skeleton {
  display: flex;
  .xtx-skeleton {
    margin-right: 10px;
    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}
.right {
  transform: translateX(-50%);
}
.left {
  transform: translateX(0);
}
.home-skeleton {
  width: 1240px;
  height: 305px;
  display: flex;
  justify-content: space-between;
  .item {
    width: 240px;
    .xtx-skeleton ~ .xtx-skeleton {
      display: block;
      margin: 16px auto 0;
    }
  }
}
</style>
